/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart.connection;

import org.eclipse.draw2d.AbstractConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

import com.cea.papyrus.diagraminterchange2.di2.GraphConnector;

/**
 * Anchor for connections/waypoints.
 * This Anchor is backuped to the model anchor
 */
public class PolylineAnchor extends AbstractConnectionAnchor
{

	  /**
  	 * 
  	 */
  	protected GraphConnector modelAnchor;
	  


/**
 * Constructs a Anchor with the given <i>owner</i> figure.
 * 
 * @param pos The position of the anchor on the specified border. The position is specified as
 * a percentage (between 0.0 and 1)
 * @param owner The owner figure
 * @param modelAnchor 
 * @param border The border where the anchor is located (NORTH, SOUTH, EAST, WEST).
 * 
 * @since 2.0
 */
public PolylineAnchor(IFigure owner, GraphConnector modelAnchor) {
	super(owner);
	this.modelAnchor = modelAnchor;
}

/**
 * Gets a Rectangle from {@link #getBox()} and returns the Point where a line from the
 * center of the Rectangle to the Point <i>reference</i> intersects the Rectangle.
 * 
 * @param reference The reference point
 * 
 * @return The anchor location
 */
public Point getLocation(Point reference) {
	   Point ref = modelAnchor.getPosition().getCopy();
		getOwner().translateToAbsolute(ref);
		return ref;
}

/**
 * Returns the bounds of this ChopboxAnchor's owner.  Subclasses can override this method
 * to adjust the box the anchor can be placed on.  For instance, the owner figure may have
 * a drop shadow that should not be included in the box.
 * 
 * @return The bounds of this ChopboxAnchor's owner
 * 
 * @since 2.0
 */
protected Rectangle getBox() {
	return getOwner().getBounds();
}

/**
 * Returns the anchor's reference point. In the case of the ChopboxAnchor, this is the
 * center of the anchor's owner.
 * 
 * @return The reference point
 */
public Point getReferencePoint() {
	   Point ref = modelAnchor.getPosition().getCopy();
		getOwner().translateToAbsolute(ref);
		return ref;
}

/**
 * Gets a Rectangle from {@link #getBox()} and returns the Point where a line from the
 * center of the Rectangle to the Point <i>reference</i> intersects the Rectangle.
 * 
 * @param reference The reference point
 * 
 * @return The anchor location
 */
public Point getBoxIntersect(Point reference) {
	Rectangle r = Rectangle.SINGLETON;
	r.setBounds(getBox());
	r.translate(-1, -1);
	r.resize(1, 1);

	getOwner().translateToAbsolute(r);
	float centerX = r.x + 0.5f * r.width;
	float centerY = r.y + 0.5f * r.height;
	
	if (r.isEmpty() || (reference.x == (int)centerX && reference.y == (int)centerY))
		return new Point((int)centerX, (int)centerY);  //This avoids divide-by-zero

	float dx = reference.x - centerX;
	float dy = reference.y - centerY;
	
	//r.width, r.height, dx, and dy are guaranteed to be non-zero. 
	float scale = 0.5f / Math.max(Math.abs(dx) / r.width, Math.abs(dy) / r.height);

	dx *= scale;
	dy *= scale;
	centerX += dx;
	centerY += dy;

	return new Point(Math.round(centerX), Math.round(centerY));
}

/**
 * Returns the anchor's reference point. In the case of the ChopboxAnchor, this is the
 * center of the anchor's owner.
 * 
 * @return The reference point
 */
public Point getAbsoluteCenter() {
	Point ref = getBox().getCenter();
	getOwner().translateToAbsolute(ref);
	return ref;
}



/**
 * 
 * 
 * @return 
 */
public GraphConnector getModelAnchor() {
	return modelAnchor;
}

}
